﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using Caliburn.Micro;
using OPSYS_Schematic.BussinessLogic.EventMessage;
using OPSYS_Schematic.ShapeFoundation;
using WPFSchematics.Graphics.Views;

namespace OPSYS_Schematic.BussinessLogic
{
    /*******************************************************************
     * * 文件名称：DesignerViewManager.cs
     * * 功   能： 管理Presentation层中DesignerView的图形操作和处理菜单命令
     * * 作   者：yuananyun
     * * 创建日期：2012年3月19日
     * * 修改日期：
     * * 修改人：
     * *******************************************************************/

    [Export(typeof (IDesignerViewManager))]
    public class DesignerViewManager : IDesignerViewManager, IHandle<IShapeOperationMessage>
    {
        public DesignerViewManager()
        {
            ShapeViews = new List<ShapeView> {new ShapeCanvas(),new ShapeCanvas()};
            var eventAggregator = IoC.GetInstance(typeof (IEventAggregator), null) as IEventAggregator;
            if (eventAggregator != null)
                eventAggregator.Subscribe(this); //订阅菜单点击事件
            else
            {
                throw new Exception("事件监听对象不能为空！");
            }
        }

        #region Implementation of IDesignerViewManager

        /// <summary>
        /// 当前激活的ShapeView
        /// </summary>
        private ShapeView _currentShapeView;
        public ShapeView CurrentShapeView
        {
            get { return _currentShapeView ?? (_currentShapeView = ShapeViews.First()); }
            set { _currentShapeView = value; }
        }

        public ShapeView SecondShapeView
        {
            get { return ShapeViews.LastOrDefault(); }
            set {  }
        }

        public IList<ShapeView> ShapeViews { get; set; }



        /// <summary>
        /// 激活指定的ShapeView
        /// </summary>
        /// <param name="shapeView"></param>
        /// <returns></returns>
        public bool ActiveShapeView(ShapeView shapeView)
        {
            try
            {
                ShapeView activeShapeView = ShapeViews.FirstOrDefault(x => x.Naming.TID == shapeView.Naming.TID);
                if (activeShapeView != null && activeShapeView != CurrentShapeView)
                    CurrentShapeView = activeShapeView;
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 添加ShapeView
        /// </summary>
        /// <param name="shapeView"></param>
        public void AddShapeView(ShapeView shapeView)
        {
            ShapeViews.Add(shapeView);
        }

        /// <summary>
        /// 移除指定的ShapeView
        /// </summary>
        /// <param name="shapeView"></param>
        public void RemoveShapeView(ShapeView shapeView)
        {
            ShapeView removeShapeView = ShapeViews.FirstOrDefault(x => x.Naming.TID == shapeView.Naming.TID);
            if (removeShapeView != null) ShapeViews.Remove(removeShapeView);
        }

        /// <summary>
        /// 清除所有的ShapeView
        /// </summary>
        public void Clear()
        {
            ShapeViews.Clear();
        }

        #endregion

        #region Implementation of IHandle<IShapeOperationMessage>

        /// <summary>
        ///   Handles the message.
        /// </summary>
        /// <param name = "message">The message.</param>
        public void Handle(IShapeOperationMessage message)
        {
            if (message == null) throw new ArgumentException("参数不能为空");
            if (CurrentShapeView != null)
                message.ShapeOperation.OperateFunction(CurrentShapeView, null);
        }

        #endregion
    }
}